package com.shouer.doc.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Build;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by ZXL on 2016/12/6.
 */

public class TimeText extends TextView{
    private CountDownTimer countDownTimer;

    private OnCountDownFinishListener onCountDownFinishListener;

    private final int DEFAULT_COUNTDOWN_TIME = 60 * 1000;

    private int countdown_time = DEFAULT_COUNTDOWN_TIME;
    private String mDefaultTxt = "";

    public TimeText(Context context) {
        super(context);
        init();
    }

    public TimeText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TimeText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public TimeText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        mDefaultTxt = getText().toString();
    }

    private void setDefault_txt(String default_txt) {
        mDefaultTxt = default_txt;
    }

    private void setCountdown_time(int countdown_time) {
        this.countdown_time = countdown_time;
    }

    @Override
    public boolean callOnClick() {
        return super.callOnClick();
    }

    public void cancelCountDownTimer()
    {
        setClickable(true);
        if (null!=countDownTimer)
        {
            countDownTimer.cancel();
            countDownTimer=null;
        }
        setText(mDefaultTxt);
    }

    public void startCountDownTimer() {

        setClickable(false);
        if (null == countDownTimer) {
            countDownTimer = new CountDownTimer(countdown_time, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {

                    long unFinished = millisUntilFinished / 1000;
                    String countDownTxt = unFinished + " S";

                    setText(countDownTxt);
                }

                @Override
                public void onFinish() {
                    setClickable(true);
                    setText(mDefaultTxt);
                    if (null != onCountDownFinishListener)
                        onCountDownFinishListener.onCountDownFinish();
                }
            };
        }
        countDownTimer.start();
    }

    @Override
    public void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w,h,oldw,oldh);
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
    }

    @Override
    public void onDetachedFromWindow() {
        if (null != countDownTimer) {
            countDownTimer.cancel();
        }
        super.onDetachedFromWindow();
    }

    public OnCountDownFinishListener getOnCountDownFinishListener() {
        return onCountDownFinishListener;
    }

    public void setOnCountDownFinishListener(OnCountDownFinishListener onCountDownFinishListener) {
        this.onCountDownFinishListener = onCountDownFinishListener;
    }

    public interface OnCountDownFinishListener {
        public abstract void onCountDownFinish();
    }
}
